<!--
 * @Description: 系统通知配置
-->
<template>
  <BaseMainSlot>
    <template v-slot:btn-box>
      <ActionBar
        :page-no="94"
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :has-cancel="false"
        :has-save="false"
        :has-edit="false"
        :save-active="saveActive"
        @add="add(undefined,'queryDataList')"
        @cancel="cancel"
        @edit="edit"
        @del="del"
        @save="save"
      />
    </template>
    <template v-slot:search-box>
      <SearchForm :formConfig="formConfig" :formData="searchlist" @search="search"></SearchForm>
      <!-- <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">通知名称:</div>
          <div class="input">
            <el-input v-model="searchlist.noticeConfigName" clearable
              @blur="search"  @keyup.enter.native="search" @clear="search" />
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">联系人:</div>
          <div class="input">
            <el-select v-model="searchlist.personNo" @change="search" clearable filterable>
              <el-option
                v-for="items in StaffData"
                :key="items.personNo"
                :label="items.contactsName"
                :value="items.personNo"
              />
            </el-select>
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">通知方式:</div>
          <div class="input">
            <el-input v-model="searchlist.noticeWay" clearable
              @blur="search"  @keyup.enter.native="search" @clear="search" />
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">通知类型:</div>
          <div class="input">
            <el-radio
              v-model="searchlist.noticeType"
              :label="true"
              @change="search"
            >必须送达</el-radio>
            <el-radio
              v-model="searchlist.noticeType"
              :label="false"
              @change="search"
            >允许丢失</el-radio>
          </div>
        </div>
      </SearchBox> -->
    </template>
    <template v-slot:table-box>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="calc(100% - 50px)"
        @row-click="handleEdit"
        @selection-change="handleSelectionChange"
        @sort-change="changeTableSort"
      >
        <el-table-column type="selection" width="55" :selectable="row => { return !row.indflag }" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="noticeConfigName" label="通知名称" />
        <el-table-column prop="noticeType" label="通知类型">
          <template
            v-if="scope.row.noticeType !== undefined"
            slot-scope="scope"
          >
            <span v-if="scope.row.noticeType">必须送达</span>
            <span v-if="!scope.row.noticeType">允许丢失</span>
          </template>
        </el-table-column>
        <el-table-column width="200" label="操作">
          <template v-if="scope.row.noticeConfigNo" slot-scope="scope">
            <el-button
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="dbHandleEdit(scope.row)"
            ><i class="el-icon-view"></i> 查看</el-button>
            <el-button
              v-if="judgePermission(94,'edit')"
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="edit"
            ><i class="el-icon-edit-outline"></i> 修改</el-button>
            <el-button
              v-if="judgePermission(94,'delete')"
              class="text-btn-cursor cursor-delete"
              type="text"
              size="small"
              @click="handleDelete(scope.row.noticeConfigNo)"
            ><i class="el-icon-delete"></i> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="titleText + '系统通知信息'"
        :visible.sync="dialogFormVisible"
        :before-close="cancel"
        width="40%"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          :show-message="canInput"
          class="el-form"
        >
          <el-form-item label="通知名称:" class="w100p" prop="noticeConfigName">
            <el-input v-model="formData.noticeConfigName" clearable />
          </el-form-item>
          <el-form-item label="通知人:" class="w100p" :required="true">
            <div class="tifier-add">
              <el-button
                type="text"
                size="medium"
                @click="tifierAdd"
              ><i class="el-icon-circle-plus"></i> 增加</el-button>
            </div>
            <div
              v-for="(item, index) in NotifierList"
              :key="index"
              class="w100p phone"
            >
              <el-select
                v-model="item.personNo"
                class="w50p"
                clearable
                filterable
                @change="personData(item, index)"
              >
                <el-option
                  v-for="items in StaffData"
                  :key="items.personNo"
                  :label="items.contactsName"
                  :value="items.personNo"
                />
              </el-select>
              <div class="w50p">
                <!-- <el-checkbox
                  :indeterminate="isIndeterminate"
                >全选</el-checkbox> -->
                <el-radio
                  v-model="item.radio"
                  :label="1"
                  @change="tifierChange(item.radio, index)"
                >全选</el-radio>
                <el-radio
                  v-model="item.radio"
                  :label="2"
                  @change="tifierChange(item.radio, index)"
                >全不选</el-radio>
                <el-button
                  class="text-btn-cursor cursor-delete"
                  type="text"
                  size="small"
                  @click="tifierDelete(item, index)"
                ><i class="el-icon-delete"></i> 删除</el-button>
              </div>
              <div class="w100p">
                <el-checkbox
                  v-model="item.cidFlag"
                  size="medium"
                >APP</el-checkbox>
                <!-- <el-checkbox
                  v-model="item.weChatFlag"
                  size="medium"
                >微信</el-checkbox> -->
                <el-checkbox
                  v-model="item.phoneFlag"
                  size="medium"
                >手机</el-checkbox>
                <el-checkbox
                  v-model="item.emailFlag"
                  size="medium"
                >邮箱</el-checkbox>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="通知类型:" class="w100p radio-label">
            <el-radio
              v-model="formData.noticeType"
              :label="true"
            >必须送达</el-radio>
            <el-radio
              v-model="formData.noticeType"
              :label="false"
            >允许丢失</el-radio>
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
      <el-dialog
        :title="'查看' + '告警联系人信息'"
        :visible.sync="dialogNoticeFormVisible"
        width="40%"
        :before-close="cancel"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          :show-message="canInput"
          class="el-form"
        >
          <el-form-item label="通知名称:" class="w100p" prop="noticeConfigName">
            <el-input v-model="formData.noticeConfigName" disabled clearable />
          </el-form-item>
          <el-form-item label="通知人:" class="w100p" disabled :required="true">
            <div
              v-for="(item, index) in NotifierList"
              :key="index"
              class="w100p phone"
            >
              <el-select
                v-model="item.personNo"
                class="w50p"
                clearable
                filterable
                disabled
                @change="personData(item, index)"
              >
                <el-option
                  v-for="items in StaffData"
                  :key="items.personNo"
                  :label="items.contactsName"
                  :value="items.personNo"
                />
              </el-select>
              <div class="w50p">
                <!-- <el-checkbox
                  :indeterminate="isIndeterminate"
                >全选</el-checkbox> -->
                <el-radio
                  v-model="item.radio"
                  :label="1"
                  disabled
                  @change="tifierChange(item.radio, index)"
                >全选</el-radio>
                <el-radio
                  v-model="item.radio"
                  :label="2"
                  disabled
                  @change="tifierChange(item.radio, index)"
                >全不选</el-radio>
              </div>
              <div class="w100p">
                <el-checkbox
                  v-model="item.cidFlag"
                  disabled
                  size="medium"
                >APP</el-checkbox>
                <!-- <el-checkbox
                  v-model="item.weChatFlag"
                  disabled
                  size="medium"
                >微信</el-checkbox> -->
                <el-checkbox
                  v-model="item.phoneFlag"
                  disabled
                  size="medium"
                >手机</el-checkbox>
                <el-checkbox
                  v-model="item.emailFlag"
                  disabled
                  size="medium"
                >邮箱</el-checkbox>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="通知类型:" class="w100p radio-label">
            <el-radio
              v-model="formData.noticeType"
              :label="true"
              disabled
            >必须送达</el-radio>
            <el-radio
              v-model="formData.noticeType"
              disabled
              :label="false"
            >允许丢失</el-radio>
          </el-form-item>
        </el-form>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import SearchForm from '@/components/searchForm';
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import nowpagereq from "@/api/digital-production/monitoring-alarm/system-notification-configuration";
import nowalarm from "@/api/digital-production/monitoring-alarm/alarm-contact-management";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot";
import { mymixin } from "@/utils/mixins";
import { mapGetters } from 'vuex';
export default {
  name: "SystemNotificationConfiguration",
  components: { SearchForm, ActionBar, Pagination, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      formConfig: [
        {
          label: '通知名称',
          prop: 'noticeConfigName',
          type: 'input',
        }, {
          label: '联系人',
          prop: 'personNo',
          type: 'select',
          options: [], // StaffData
          optionKey: { label: 'contactsName', value: 'personNo' }
        }, {
          label: '通知方式',
          prop: 'noticeWay',
          type: 'input',
        }, {
          label: '通知类型',
          prop: 'noticeType',
          type: 'radio',
          radios: [
            { label: '必须送达', value: true },
            { label: '允许丢失', value: false },
          ]
        },
      ],
      rules: {
        noticeConfigName: [
          { required: true, message: "请填写姓名", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "请填写手机号", trigger: "blur" },
          { type: "number", message: "手机号为数字" }
        ]
      },
      // 搜索内容
      searchlist: {},
      // 选中的数据项数组 alarmContactId组成 (此数组最多为一个)
      selectIdList: [],
      NotifierList: [
        {
          userName: "",
          personNo: "",
          phoneFlag: false,
          weChatFlag: false,
          emailFlag: false,
          cidFlag: false,
          radio: 3
        }
      ],
      StaffData: [],
      dialogTitle: "",
      dialogFormVisible: false,
      dialogNoticeFormVisible: false
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {
    nowChanceStau: {
      handler(val) {
        if (val === "") {
          this.NotifierList = [
            {
              userName: "",
              personNo: "",
              phoneFlag: false,
              weChatFlag: false,
              emailFlag: false,
              cidFlag: false,
              radio: 3
            }
          ];
        }
      }
    }
  },
  created() {
    // 获取表格数据
    this.getTableDataList();
    this.queryDataList();
  },
  mounted() {},
  methods: {
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        sortList: this.sortList || [],
        entity: {
          ...this.searchlist,
          organizationNo: sessionStorage.organizationNo
        }
      };

      nowpagereq.getlist(params).then((res) => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error("查询失败");
        }
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
        this.addblankmet();
        this.resetActionBar();
      });
    },
    async queryDataList() {
      // 员工
      const { data: Staff } = await nowalarm.querybycondition();
      this.StaffData = Staff;
      this.formConfig[1].options = Staff;
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    // // 添加
    // add() {
    //   this.openDialog("add");
    //   this.formData = {};
    // },
    // // 取消
    // cancel() {
    //   this.closeDialog();
    //   this.formData = {};
    // },
    personData(val, index) {
      this.StaffData.forEach((item) => {
        if (val.personNo === item.personNo) {
          console.log(val);
          this.$set(this.NotifierList, index, {
            ...val,
            userName: item.userName
          });
        }
      });
    },
    tifierChange(item, index) {
      if (item === 1) {
        this.$set(this.NotifierList, index, {
          cidFlag: true,
          weChatFlag: true,
          phoneFlag: true,
          radio: 1,
          emailFlag: true
        });
      } else if (item === 2) {
        this.$set(this.NotifierList, index, {
          cidFlag: false,
          weChatFlag: false,
          phoneFlag: false,
          radio: 2,
          emailFlag: false
        });
      } else {
        this.$set(this.NotifierList, index, {
          radio: 3
        });
      }
    },
    tifierDelete(item, index) {
      this.NotifierList.splice(index, 1);
    },
    tifierAdd() {
      console.log(this.NotifierList,this.StaffData); 
      this.NotifierList.forEach(el=>{
        this.StaffData = this.StaffData.filter(ell=> el.personNo !== ell.personNo)
      })
      this.NotifierList.push({
        userName: "",
        personNo: "",
        phoneFlag: false,
        weChatFlag: false,
        emailFlag: false,
        cidFlag: false,
        radio: 3
      });
    },
    async dbHandleEdit(row) {
      const { data: rs } = await nowpagereq.queryByNo(row.noticeConfigNo);
      this.formData = { ...rs};
      this.NotifierList = rs.noticeMethodList;
      console.log(this.NotifierList, 2, this.formData)
      this.dialogNoticeFormVisible = true;
    },
    async handleEdit(row) {
      const { data: rs } = await nowpagereq.queryByNo(row.noticeConfigNo);
      this.formData = JSON.parse(JSON.stringify(rs));
      this.NotifierList = rs.noticeMethodList;
      // this.NotifierList = row.noticeMethodList
    },
    handleSelectionChange(list) {
      this.selectIdList = [];
      let notvalue = true;
      this.selectIdList = list.map((item) => {
        if (item.noticeConfigNo) {
          return item.noticeConfigNo;
        } else {
          notvalue = false;
          return "";
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
        return;
      }
      if (this.selectIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      nowpagereq.dellist(this.selectIdList).then((res) => {
        if (res.code === 1) {
          this.$message.success("删除成功");
          this.getTableDataList();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    handleDelete(noticeConfigNo) {
      // nowpagereq.del(noticeConfigNo).then((res) => {
      //   if (res.code === 1) {
      //     this.$message.success("删除成功");
      //     this.getTableDataList();
      //   } else {
      //     this.$message.error("删除失败");
      //   }
      // });
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          nowpagereq.del(noticeConfigNo).then(res => {
            if (res.code === 1) {
              this.$message.success("删除成功");
              this.getTableDataList();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          //
          this.$message("取消删除");
        });
    },
    save() {
      if (this.nowChanceStau === "edit") {
        // 编辑接口
        // this.formData.routePointMapperList = this.tagsList;
        nowpagereq.editlist(this.formData).then((res) => {
          if (res.code === 1) {
            this.$message.success("修改成功！");
            this.closeDialog();
            this.getTableDataList();
          } else {
            this.$message.error("修改失败！");
          }
        });
      } else if (this.nowChanceStau === "add") {
        if (this.NotifierList.length === 0) {
          return this.$message.warning("通知人不能为空");
        }
        if (this.NotifierList[0].personNo === "") {
          return this.$message.warning("通知人不能为空");
        }
        this.formData.noticeMethodList = this.NotifierList;
        this.formData.organizationNo = sessionStorage.organizationNo;
        nowpagereq.addlist(this.formData).then((res) => {
          if (res.code === 1) {
            this.$message.success("新增成功！");
            this.closeDialog();
            this.getTableDataList();
          } else {
            this.$message.error("新增失败！");
          }
        });
      }
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = "";
    }
  }
};
</script>

<style scoped lang="scss">
.phone {
  width: 100%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  margin: 5px;
  justify-content: space-between;
  .el-select {
    width: 48%;
  }
}
.tifier-add {
  position: absolute;
  left: -60px;
  top: 32px;
}
.radio-label {
  ::v-deep {
    .el-form-item__content {
      padding-top: 10px;
    }
  } 
}
</style>
